package com.crawler.search.index.keyword;

import com.alibaba.fastjson.JSONArray;
import com.crawler.search.constant.QuerierMapper;
import com.crawler.search.doc.entity.DocField;
import com.google.common.collect.Lists;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * 高级检索
 * @Class Name KeywordUtil
 * @author 张永辉
 * @Create In 2018年8月7日
 */
public class KeywordUtil {

    public static List<Node> getTree(String json) {

    	List<Node> nodes = new ArrayList<Node>();
    	if(StringUtils.isEmpty(json)){
    		return nodes;
		}

		List<WordField> wordFields = JSONArray.parseArray(json, WordField.class);
    	if(!CollectionUtils.isEmpty(wordFields)){
			for (int i = 0; i < wordFields.size(); i++) {
				WordField word = wordFields.get(i);
				parseLogic(word,i,nodes);
			}
		}
    	return nodes;
    }

    public static void parseLogic(WordField wordField,int i,List<Node> nodes){
		Node node = new Node();
		String logic = wordField.getLogic();
		if(StringUtils.isEmpty(logic)&&i==0){
			node.setType(KeywordType.must.toString());
		}else{
			node.setType(KeywordType.from(logic).toString());
		}
		node.setFiled(wordField.getKey());
		node.setWord(wordField.getValue());
		nodes.add(node);
	}
}


